package com.sm.healthy.adapter

import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseDataBindingHolder
import com.sm.healthy.R
import com.sm.healthy.data.bean.UserEntity
import com.sm.healthy.databinding.ItemUserCenterLayoutBinding

/**
 * @description 个人中心列表
 * @author csw
 * @date 2021-12-03
 */
class UserListAdapter(
    layoutId: Int = R.layout.item_user_center_layout,
    list: MutableList<UserEntity> = arrayListOf()
) : BaseQuickAdapter<UserEntity, BaseDataBindingHolder<ItemUserCenterLayoutBinding>>(
    layoutId,
    list
) {

    var selectPosition = -1
        set(value) {
            val old = field
            field = if (old == value) -1 else value
            notifyDataSetChanged()
        }

    var curSelectPosition = -1
        set(value) {
            val old = field
            field = if (old == value) -1 else value
            notifyDataSetChanged()
        }

    override fun setOnItemLongClick(v: View, position: Int): Boolean {
        selectPosition = position
        return super.setOnItemLongClick(v, position)
    }

    override fun convert(
        holder: BaseDataBindingHolder<ItemUserCenterLayoutBinding>,
        item: UserEntity
    ) {
        with(holder.dataBinding) {
            this?.del = selectPosition == holder.absoluteAdapterPosition
            this?.select = curSelectPosition == holder.absoluteAdapterPosition
        }
        holder.dataBinding?.userinfo = item
        holder.dataBinding?.executePendingBindings()
    }

}